﻿using System;
using System.Linq;
using System.Windows;
using CCWP7.Model;
using System.Collections.Generic;
using Caliburn.Micro;

namespace CCWP7.CaliburnMicro
{
    public class AddServerPageViewModel : PropertyChangedBase
    {
        readonly INavigationService _navigationService;
        readonly IServerDataManager _serverDataManager;
        readonly Func<ReadServer> _readServerFactory;

        public AddServerPageViewModel(INavigationService navigationService, IServerDataManager serverDataManager, Func<ReadServer> readServerFactory)
        {
            _navigationService = navigationService;
            _serverDataManager = serverDataManager;
            _readServerFactory = readServerFactory;

            _fullAddress = new Uri("http://ccnet.wheelmud.net/XmlServerReport.aspx");

            Refresh();
        }

        Uri _fullAddress;
        public string ServerName
        {
            get
            {
                return _fullAddress != null ? _fullAddress.Host : string.Empty;
            }
            set
            {
                if (!string.IsNullOrEmpty(value))
                {
                    if (_fullAddress != null)
                    {
                        var ub = new UriBuilder(_fullAddress) { Host = value };

                        SetFullAddress(ub.Uri);
                    }
                    else
                        SetFullAddress(new Uri(string.Concat("http://", value, "/XmlServerReport.aspx"), UriKind.Absolute));
                }
                else
                    SetFullAddress(null);
            }
        }

        public int PortNumber
        {
            get
            {
                return _fullAddress != null ? _fullAddress.Port : 80;
            }
            set
            {
                if (value <= 0)
                    SetFullAddress(null);
                else
                {
                    if (_fullAddress != null)
                    {
                        var ub = new UriBuilder(_fullAddress) { Port = value };

                        SetFullAddress(ub.Uri);
                    }
                }
            }
        }

        public string FullAddress
        {
            get
            {
                return _fullAddress != null ? _fullAddress.ToString() : string.Empty;
            }
            set
            {
                if (string.IsNullOrEmpty(value))
                    SetFullAddress(null);
                else
                    SetFullAddress(new Uri(value));
            }
        }

        void SetFullAddress(Uri uri)
        {
            _fullAddress = uri;

            CanAddServer = _fullAddress != null && _fullAddress.IsAbsoluteUri;

            Refresh();
        }

        public bool CanAddServer { get; private set; }
        public IEnumerable<IResult> AddServer()
        {
            CanAddServer = false;
            Refresh();

            var uriAdded = new Uri(FullAddress);
            if (_serverDataManager.Servers.Any(s => new Uri(s.Uri) == uriAdded))
                MessageBox.Show("That server is already being monitored", "Can't add server", MessageBoxButton.OK);
            else
            {
                yield return new SetVisualState("Adding");

                var readServer = _readServerFactory();
                readServer.Server.Name = ServerName;
                readServer.Server.Uri = FullAddress;

                yield return readServer;

                if (readServer.Server.Report != null)
                {
                    _serverDataManager.AddServer(readServer.Server);

                    yield return new SetVisualState("Default");

                    CanAddServer = true;
                    _navigationService.GoBack();
                }
                else
                {
                    MessageBox.Show("Could not read the server status");

                    yield return new SetVisualState("Default");

                    CanAddServer = false;
                }
            }
        }
        public void Cancel()
        {
            _navigationService.GoBack();
        }
    }
}
